using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Banshee.Base;
using Banshee.Widgets;
using Banshee.AudioProfiles;
using Banshee.Configuration.Schema;
using System.IO;

namespace Banshee.Base.Winforms.Gui
{
    public partial class PreferencesDialog : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private ProfileManager manager;
        //private DictionaryComboBox<string> folder_box;
        //private DictionaryComboBox<string> file_box;
        public PreferencesDialog()
        {
            InitializeComponent();
        }

        private void PreferencesDialog_Load(object sender, EventArgs e)
        {
            manager = Globals.AudioProfileManager;
            BuildWindow();
        }
        void BuildWindow()
        {
            CreateFileSystemEntries();
            string file_pattern = LibrarySchema.FilePattern.Get(FileNamePattern.DefaultFile);
            string folder_pattern = LibrarySchema.FolderPattern.Get(FileNamePattern.DefaultFolder);
            bool preference_already_added;
            preference_already_added = false;
            foreach (string pattern in FileNamePattern.SuggestedFolders)
            {
                if (pattern.Equals(folder_pattern))
                {
                    preference_already_added = true;
                }
                folder_box.Items.Add(FileNamePattern.CreatePatternDescription(pattern));
                //folder_box.Items.
            }
            preference_already_added = false;
            foreach (string pattern in FileNamePattern.SuggestedFiles)
            {
                if (pattern.Equals(file_pattern))
                {
                    preference_already_added = true;
                }
                file_box.Items.Add(FileNamePattern.CreatePatternDescription(pattern));
            }
            LoadPreferences();
            ReloadProfiles();
        }
        public string[] MimeTypeFilter
        {
            get { return mimetype_filter; }
            set
            {
                mimetype_filter = value;
                ReloadProfiles();
            }
        }
        private string[] mimetype_filter;
        public void ReloadProfiles()
        {
            ComboBox combo = cd_importing_profile_box;
            List<Profile> mimetype_profiles = new List<Profile>();

            if (mimetype_filter != null && mimetype_filter.Length > 0)
            {
                foreach (string mimetype in mimetype_filter)
                {
                    Profile profile = manager.GetProfileForMimeType(mimetype);
                    if (profile != null && !mimetype_profiles.Contains(profile))
                    {
                        mimetype_profiles.Add(profile);
                    }
                }
            }

            if (manager.AvailableProfileCount == 0 || (mimetype_profiles.Count == 0 && mimetype_filter != null))
            {
                
                combo.Items.Add("No available profiles");
            }
            else
            {
                //Sensitive = true;
            }

            if (mimetype_profiles.Count > 0)
            {
                foreach (Profile profile in mimetype_profiles)
                {
                    combo.Items.Add(String.Format("{0}", profile.Name));
                }
            }
            else
            {
                foreach (Profile profile in manager.GetAvailableProfiles())
                {
                    combo.Items.Add(String.Format("{0}", profile.Name));
                }
            }
            combo.SelectedIndex = 0;

            /*if (store.IterNthChild(out active_iter, 0))
            {
                SetActiveIter(active_iter);
            }*/
        }
        void CreateFileSystemEntries()
        {
            library_location_chooser.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            library_location_chooser.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            library_location_chooser.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
            library_location_chooser.Items.Add("Other");
        }
        private void LoadPreferences()
        {
            //IconThemeUtils.SetWindowIcon(this, Properties.Resources.music_player_banshee_22);
            string location = Globals.Library.Location;
            if (!Directory.Exists(location))
            {
                location = Paths.DefaultLibraryPath;
            }

            if (library_location_chooser.Items.Contains(location))
                library_location_chooser.SelectedItem = location;
            else
            {
                library_location_chooser.Items.Add(location);
                 library_location_chooser.SelectedItem = location;
            }
            Globals.Library.Location = location;
            string folderpat = LibrarySchema.FilePattern.Get();
            file_box.SelectedItem = LibrarySchema.FilePattern.Get();
            if (string.IsNullOrEmpty(LibrarySchema.FilePattern.Get()))
                file_box.SelectedIndex = 0;
            
            folder_box.SelectedItem = LibrarySchema.FolderPattern.Get();
            if (string.IsNullOrEmpty(LibrarySchema.FolderPattern.Get()))
                folder_box.SelectedIndex = 0;
            SetFileFolder();

            copy_on_import.Checked = LibrarySchema.CopyOnImport.Get();
            write_metadata.Checked = LibrarySchema.WriteMetadata.Get();
            error_correction.Checked = ImportSchema.AudioCDErrorCorrection.Get();
        }

        private void copy_on_import_CheckedChanged(object sender, EventArgs e)
        {
            LibrarySchema.CopyOnImport.Set(copy_on_import.Checked);
        }

        private void write_metadata_CheckedChanged(object sender, EventArgs e)
        {
            LibrarySchema.WriteMetadata.Set(write_metadata.Checked);
        }

        private void library_location_chooser_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LibrarySchema.Location.Set(library_location_chooser.SelectedItem.ToString());
        }

        private void folder_box_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SetFileFolder();
        }

        private void file_box_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SetFileFolder();
        }
        void SetFileFolder()
        {
            LibrarySchema.FolderPattern.Set(folder_box.SelectedItem.ToString());
            LibrarySchema.FilePattern.Set(file_box.SelectedItem.ToString());
           example_path.Text = String.Format("{0}.ogg",FileNamePattern.CreateFromTrackInfo(
                    FileNamePattern.CreateFolderFilePattern(folder_box.SelectedItem.ToString(),
                        file_box.SelectedItem.ToString()), new SampleTrackInfo()));
        }
        private void error_correction_CheckedChanged(object sender, EventArgs e)
        {
            ImportSchema.AudioCDErrorCorrection.Set(error_correction.Checked);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}